package com.fengye.sphere.service.user.controller;

import com.fengye.sphere.api.domain.vo.UserVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author gaohaiqing
 * @since 2023/12/9
 */
@RestController
public class UserController {

    private static final List<UserVO> userVOS = new ArrayList<>();

    UserController() {
        userVOS.add(UserVO.builder().id(1L).name("张三").build());
        userVOS.add(UserVO.builder().id(2L).name("李四").build());
        userVOS.add(UserVO.builder().id(3L).name("王五").build());
    }

    @GetMapping("/users")
    public List<UserVO> userList() {
        return userVOS;
    }

    @GetMapping("/user/{id}")
    public UserVO user(@PathVariable(value = "id") Long id) {
        return userVOS.stream().filter(userVO -> Objects.equals(userVO.getId(), id))
                .findFirst().orElseThrow(() -> new RuntimeException("未找到该用户"));
    }
}